﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Repo7.BusinessLogic.Network.Cluster
{
    /// <summary>
    /// Repository Peer Info
    /// </summary>
    public class RepoPeerInfo
    {
        #region Properties
        
        /// <summary>
        /// End Point for Commands
        /// </summary>
        public IPEndPoint CommandEndPont { get; set; }

        /// <summary>
        /// File Transfer End Point
        /// </summary>
        public IPEndPoint FileTransferEndPoint { get; set; }

        /// <summary>
        /// Name of the Peer
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Peer is Online
        /// </summary>
        public bool IsOnline { get; set; }

        /// <summary>
        /// Is the current node the Master 
        /// </summary>
        public bool IsMaster { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Empty constructor
        /// </summary>
        public RepoPeerInfo() { }

        /// <summary>
        /// Constructor with Name parameter
        /// </summary>
        /// <param name="name">Peer Name</param>
        public RepoPeerInfo(string name)
        {
            this.Name = name;
        }

        #endregion

        #region Pulic Methods

        /// <summary>
        /// Try to set the file transfer end point.
        /// </summary>
        /// <param name="fileTransferEndPoint">IP End Point</param>
        /// <returns>Operation Successful</returns>
        public bool TrySetFileTransferEndPoint(string fileTransferEndPoint)
        {
            if (string.IsNullOrEmpty(fileTransferEndPoint))
            {
                return false;
            }

            try
            {
                FileTransferEndPoint = GetEndPointFromString(fileTransferEndPoint);
                return true;
            }
            catch (Exception)
            {
                //operation not successful
                return false;
            }
        }

        /// <summary>
        /// Try to set the command end point.
        /// </summary>
        /// <param name="fileTransferEndPoint">IP End Point</param>
        /// <returns>Operation Successful</returns>
        public bool TrySetCommandEndPoint(string commandEndPoint)
        {
            if (string.IsNullOrEmpty(commandEndPoint))
            {
                return false;
            }

            try
            {
                CommandEndPont = GetEndPointFromString(commandEndPoint);
                return true;
            }
            catch (Exception)
            {
                //operation not successful
                return false;
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Get an IPEndPoint from a string
        /// </summary>
        /// <param name="endPoint"></param>
        /// <returns></returns>
        private IPEndPoint GetEndPointFromString(string endPoint)
        {
            string address = endPoint;
            int port = 45678; // default file transfer port

            int startOfPort = endPoint.IndexOf(':');
            if (startOfPort != -1)
            {
                port = int.Parse(endPoint.Substring(startOfPort + 1));
                address = endPoint.Substring(0, startOfPort);
            }

            IPAddress ipAddress = IPAddress.Parse(address);
            return new IPEndPoint(ipAddress, port); 
        }

        #endregion
    }
}
